package com.njwb.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Description: 类的描述
 * Author: yuandeyin@itany.com
 * Date: 2025-08-08 15:30
 * Version: 1.0
 */
public class TestDeleteDept {
    public static void main(String[] args) {
        //准备删除部门编号为30的部门信息
        int deptNo = 30;

        //1.加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //2.获取连接
        Connection conn = null;
        PreparedStatement pstmt = null;
        String url = "jdbc:mysql://localhost:3306/db_test?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT";
        try {
            conn = DriverManager.getConnection(url,"root","root");
            //3.获取SQL执行器
            String sql = "delete from dept where deptno = ?";
            pstmt = conn.prepareStatement(sql);

            pstmt.setInt(1,deptNo);
            //4.执行SQL
            int rowCount = pstmt.executeUpdate();//delete
            if(rowCount > 0){
                //5.反馈结果
                System.out.println("删除部门成功");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //6.关闭资源
            if(null != pstmt){
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(null != conn){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }




    }
}
